package com.xiaohong.business.controller;


import com.xiaohong.account.feign.AccountTblFeign;
import com.xiaohong.account.pojo.AccountTbl;
import com.xiaohong.order.feign.OrderTblFeign;
import com.xiaohong.order.pojo.OrderTbl;
import com.xiaohong.storage.feign.StorageTblFeign;
import com.xiaohong.storage.pojo.StorageTbl;
import entity.Result;
import entity.StatusCode;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : xiaohong
 * @date : 2021/4/2 8:45
 */
@Tag(name = "Business接口文档")
@RestController
@RequestMapping("/business")
@CrossOrigin
@SuppressWarnings("all") // 抑制所有警告
public class BusinessController {

    @Autowired
    private AccountTblFeign accountTblFeign;

    @Autowired
    private OrderTblFeign orderTblFeign;

    @Autowired
    private StorageTblFeign storageTblFeign;


    /***
     * 用户下单->减少库存->增加订单->减少金额
     * @param orderTbl
     * @return
     */
    @Operation(summary = "用户下单->减少库存->增加订单->减少金额",description = "所需权限：无" )
    @PostMapping
    @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
    public Result add(@RequestBody @Parameter(name = "订单对象",required = true) OrderTbl orderTbl){

//        AccountTbl accountTbl = new AccountTbl();
//        accountTbl.setUserId(orderTbl.getUserId());
//        return accountTblFeign.findList(accountTbl);

//        AccountTbl accountTbl = new AccountTbl();
//        accountTbl.setMoney(1000);
//        accountTblFeign.update(accountTbl,6);
//        return null;

        //减少库存
        StorageTbl storageTbl = new StorageTbl();
        storageTbl.setCommodityCode(orderTbl.getCommodityCode());
        Result<List<StorageTbl>> resust1 = storageTblFeign.findList(storageTbl);
        if (resust1 != null){
            List<StorageTbl> list = resust1.getData();
            StorageTbl returnStorage = new StorageTbl();
            returnStorage.setId(list.get(0).getId());
            returnStorage.setCount(list.get(0).getCount()-1);
            // 减少库存
            storageTblFeign.update(returnStorage,list.get(0).getId());
            // 增加订单
            orderTblFeign.add(orderTbl);
            //减少金额
            AccountTbl accountTbl = new AccountTbl();
            accountTbl.setUserId(orderTbl.getUserId());
            Result<List<AccountTbl>> result2 = accountTblFeign.findList(accountTbl);
            if (result2 != null){
                List<AccountTbl> accountTblList = result2.getData();
                AccountTbl returnAccount = new AccountTbl();
                returnAccount.setId(accountTblList.get(0).getId());
                returnAccount.setMoney(accountTblList.get(0).getMoney()-orderTbl.getMoney());
                accountTblFeign.update(returnAccount,accountTblList.get(0).getId());
                return new Result(true, StatusCode.OK,"下单成功");
            }else {
                throw new RuntimeException("用户不存在");
//                return new Result(true, StatusCode.ERROR,"用户不存在");
            }
        }else {
            throw new RuntimeException("商品不存在了哦");
//            return new Result(true,StatusCode.ERROR,"商品不存在了哦");
        }

    }

}
